#pragma once
#include<sqlite3.h>
#include<string>
#include<iostream>


class SqliteHelper
{
public:
    typedef int (*sql_callback)(void*,int,char**,char**);
    SqliteHelper(std::string path):_sqlpath(path),_handler(nullptr)
    {}
    ~SqliteHelper(){}
    bool Open(int falg = SQLITE_OPEN_FULLMUTEX)
    {
                                                            //可读可写              不存在则新建       默认串行     
        int ret = sqlite3_open_v2(_sqlpath.c_str(),&_handler,SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|falg,nullptr);
        if(ret != SQLITE_OK)
        {
            std::cout<<"open error"<<std::endl;
            return false;
        }
        return true;
    }
    bool Exec(const std::string &sql,sql_callback cb,void *arg)
    {
        int ret = sqlite3_exec(_handler,sql.c_str(),cb,arg,nullptr);
        if(ret!=SQLITE_OK)
        {
            std::cout<<sql<<std::endl;
            std::cout<<"执行该语句失败"<<std::endl;
            std::cout<<sqlite3_errmsg(_handler)<<std::endl;
            return false;
        }
        return true;
    }
    void Close()
    {
        if(_handler) sqlite3_close_v2(_handler);
    }
private:
    std::string _sqlpath;//数据库文件名（路径）
    sqlite3* _handler;
};